package com.mobile.service.impl;

import com.github.pagehelper.PageInfo;
import com.mobile.service.convert.MessageNotificationConverter;
import com.mobile.service.dao.DaoSupport;
import com.mobile.service.dto.MessageNotificationDTO;
import com.mobile.service.dto.MessageNotificationQueryDTO;
import com.mobile.service.entity.MessageNotification;
import com.mobile.service.inter.MessageNotificationService;
import com.mobile.service.mapper.MessageNotificationMapper;
import com.mobile.service.util.PageUtils;
import com.mobile.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 消息通知表service实现类
 */
@Service("messageNotificationService")
@Transactional
public class MessageNotificationServiceImpl implements MessageNotificationService {
    @Resource(name = "daoSupport")
    private DaoSupport dao;
    @Autowired
    private MessageNotificationConverter messageNotificationConverter;
    @Autowired
    private MessageNotificationMapper messageNotificationMapper;

    @Override
    public Map<String, Object> addMessageNotification(MessageNotificationDTO dto) throws Exception {
        Map<String, Object> map = new HashMap<>();
        messageNotificationMapper.insertMessageNotification(messageNotificationConverter.toEntity(dto));
        map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_SUCCESS);
        map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("I0011"));
        return map;
    }

    @SuppressWarnings("unchecked")
    @Override
    public PageResult<MessageNotificationDTO> getMessageNotificationListPage(MessageNotificationQueryDTO queryDTO) throws Exception {
        PageUtils.startPage(queryDTO);
        List<MessageNotificationDTO> list = messageNotificationMapper.messageNotificationlistPage(queryDTO);
        return new PageResult<MessageNotificationDTO>(list);
    }

    @Override
    public MessageNotificationDTO getMessageNotificationById(Integer id) throws Exception {
        return messageNotificationMapper.getMessageNotificationById(id);
    }

    @Override
    public Map<String, Object> updateMessageNotification(MessageNotificationDTO dto) throws Exception {
        Map<String, Object> map = new HashMap<>();
        Integer pkValue = dto.getNotificationId();
        if (pkValue == null) {
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_FAILED);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0001"));
            return map;
        }

        MessageNotificationDTO data = this.getMessageNotificationById(pkValue);
        if (Objects.isNull(data)) {
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_FAILED);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0001"));
            return map;
        }

        messageNotificationMapper.updateMessageNotification(messageNotificationConverter.toEntity(dto));
        map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_SUCCESS);
        map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("I0009"));
        return map;
    }

    @Override
    public Map<String, Object> removeMessageNotification(Integer id) throws Exception {
        Map<String, Object> map = new HashMap<>();
        messageNotificationMapper.deleteMessageNotification(id);
        map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_SUCCESS);
        map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("I0010"));
        return map;
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<MessageNotificationDTO> getMessageNotificationList(MessageNotificationQueryDTO queryDTO) throws Exception {
        return messageNotificationMapper.getMessageNotificationList(queryDTO);
    }
}
